package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * @author : ZhangHao
 * @version :1.0
 * @date :Created in 2020/1/2
 * @description : 菜单服务实现类
 */
@Slf4j
@Service
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    //通过用户名展现当前菜单
    @Override
    public List<Menu> showMenuByUserName(String username) {
        //通过用户名查询父菜单
        List<Menu> menuList = menuDao.showMenuByUserName(username);
        log.info("[父查询菜单: ]  {}",menuList);
        if (null == menuList||menuList.size()==0) {
            return null;
        }
        for (Menu menu : menuList) {
            if (null == menu.getParentMenuId()) {
                //如果该菜单的父ID为null,说明此菜单是父菜单,找他的子菜单
                List<Menu> children = menuDao.selectChildren(username, menu.getId());
                log.info("[children] : {}",children);
                menu.setChildren(children);
            }
        }
        return menuList;
    }

    //分页查询菜单
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //开启分页查询插件
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //分页查询数据库
        Page<Menu> page = menuDao.selectPageByCondition(queryPageBean.getQueryString());
        log.info("[菜单分页查询结果] Total:{},row:{}", page.getTotal(), page.getResult());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void add(Menu menu) {
        menuDao.insert(menu);
        Set<Role> roles = menu.getRoles();
        for (Role role : roles) {
            menuDao.insertMenuAndRole(menu.getId(), role.getId());
        }
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.selectById(id);
    }

    @Override
    public void edit(Menu menu, Integer[] roleIds) {
        //更新菜单基本信息
        menuDao.update(menu);
        //删除原有的关联关系
        menuDao.deleteRoleAndMenuByMenuId(menu.getId());
        //增加新的关联关系
        for (Integer roleId : roleIds) {
            menuDao.insertMenuAndRole(menu.getId(),roleId);
        }
    }

    @Override
    public List<Role> findAllRole() {
        return menuDao.selectAllRole();
    }

    @Override
    public List<Integer> findRoleIdsByMenuId(Integer id) {
        return menuDao.findRoleIdsByMenuId(id);
    }

    @Override
    public List<Menu> findFu() {
        return menuDao.selectFu();
    }

    @Override
    public void deleteById(Integer menuId) {
        //先删菜单的关联项
        menuDao.deleteRoleAndMenuByMenuId(menuId);
        //再删菜单
        menuDao.deleteById(menuId);
    }
}
